package com.example.springbootmybatis.tools;

import com.example.springbootmybatis.core.page.PageMessage;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

/**
 * 用于Object到JSON序列化的对象结构体定义
 */
@JsonInclude(Include.ALWAYS)
public class OperationResult {

    /**
     * 返回success或failure操作标识
     */
    private String type;
    /**
     * 国际化处理的返回JSON消息代码
     */
    private int code;
    /**
     * 国际化处理的返回JSON消息正文
     */
    private String message;
    /**
     * 国际化处理的返回JSON消息页面信息
     */
    private PageMessage pageMessage;
    /**
     * 返回的数据
     */
    private Object result;
    
    /**
     * 特殊展示的数据
     */
    private Object specialResult;

    /**
     * 标识操作成功与否
     */
    public enum OPERATION_RESULT_TYPE {
        success, warning, failure, confirm
    }



    public static OperationResult buildSuccessResult() {
        return new OperationResult(OPERATION_RESULT_TYPE.success,
                CodeMessage.S_SUCCESS.getCode(),
                CodeMessage.S_SUCCESS.getMessage(), null, null);
    }

    public static OperationResult buildSuccessResult(int code) {
        return new OperationResult(OPERATION_RESULT_TYPE.success,
                code, CodeMessage.getMessage(code), null, null);
    }

    public static OperationResult buildSuccessResult(String message) {
        return new OperationResult(OPERATION_RESULT_TYPE.success,
                CodeMessage.S_SUCCESS.getCode(), message, null, null);
    }

    public static OperationResult buildSuccessResult(CodeMessage codeMessage) {
        return new OperationResult(OPERATION_RESULT_TYPE.success,
                codeMessage.getCode(), codeMessage.getMessage(), null, null);
    }

    public static OperationResult buildSuccessResult(String message, Object userdata) {
        return new OperationResult(OPERATION_RESULT_TYPE.success,
                CodeMessage.S_SUCCESS.getCode(), message, userdata, null);
    }
    
    public static OperationResult buildSuccessResult(String message, Object userdata, Object specialResult) {
        return new OperationResult(OPERATION_RESULT_TYPE.success,
                CodeMessage.S_SUCCESS.getCode(), message, userdata, specialResult);
    }
    
    public static OperationResult buildSuccessResult(CodeMessage oc, Object userdata) {
        return new OperationResult(OPERATION_RESULT_TYPE.success, oc.getCode(), oc.getMessage(), userdata, null);
    }
    
    public static OperationResult buildSuccessResult(CodeMessage oc, Object userdata, Object specialResult) {
        return new OperationResult(OPERATION_RESULT_TYPE.success, oc.getCode(), oc.getMessage(), userdata, specialResult);
    }
    
    public static OperationResult buildWarningResult(String message, Object userdata) {
        return new OperationResult(OPERATION_RESULT_TYPE.warning, CodeMessage.E_ERROR.getCode(), message, userdata, null);
    }

    public static OperationResult buildWarningResult(CodeMessage oc, Object userdata) {
        return new OperationResult(OPERATION_RESULT_TYPE.warning, oc.getCode(), oc.getMessage(), userdata, null);
    }

    public static OperationResult buildWarningResult(CodeMessage oc) {
        return new OperationResult(OPERATION_RESULT_TYPE.warning, oc.getCode(), oc.getMessage(), null, null);
    }

    public static OperationResult buildFailureResult(String message) {
        return new OperationResult(OPERATION_RESULT_TYPE.failure,
                CodeMessage.E_ERROR.getCode(),
                CodeMessage.E_ERROR.getMessage(), message, null);
    }

    public static OperationResult buildFailureResult(CodeMessage oc) {
        return new OperationResult(OPERATION_RESULT_TYPE.failure,
                oc.getCode(), oc.getMessage(), null, null);
    }

    public static OperationResult buildFailureParamResult(String message) {
        return new OperationResult(OPERATION_RESULT_TYPE.failure,
                CodeMessage.E_PART_NULL_PARAM.getCode(), CodeMessage.E_PART_NULL_PARAM.getMessage()+message, null, null);
    }

    public static OperationResult buildFailureResult(CodeMessage oc, Object userdata) {
        return new OperationResult(OPERATION_RESULT_TYPE.failure,
                oc.getCode(), oc.getMessage(), userdata, null);
    }

    /**
     * 定义返回值
     */
    public OperationResult(OPERATION_RESULT_TYPE type, int code, String message, Object result, Object specialResult) {
        this.type = type.name();
        this.code = code;
        this.message = message;
        this.specialResult = specialResult;
        //判断是否为分页对象
        /*if(result instanceof Page){
        	@SuppressWarnings("unchecked")
			Page<Object> page = (Page<Object>)result;
        	this.result = page;
        	//因源码计算页数和导航页数，在大数据情况，效率低下，所以自定义只封装基本分页数据（不使用插件自带的pageInfo）
//        	this.pageInfo = page.toPageInfo();
//        	this.pageInfo.setList(null);
        }else{*/
        	this.result = result;
//        }
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public OperationResult setCode(int code) {
        this.code = code;
        return this;
    }

    public int getCode() {
        return code;
    }

	public PageMessage getPageMessage() {
		return pageMessage;
	}

	public void setPageMessage(PageMessage pageMessage) {
		this.pageMessage = pageMessage;
	}

	public Object getSpecialResult() {
		return specialResult;
	}

	public void setSpecialResult(Object specialResult) {
		this.specialResult = specialResult;
	}
    
}
